package generateFile;

import entity.User;
import service.UserService;
import service.impl.UserServiceImpl;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class UsersFile {

    public static void generateUserList(){
        UserService userService=new UserServiceImpl();
        List<User> users=userService.queryAllStudents();
        FileWriter out=null;
        try {
            String path="D:\\Documents\\Desktop\\材料\\IDEA_workspace\\webLibrarySystem\\fileList\\用户名单.txt";
            out=new FileWriter(path);
            out.write("用户ID\t\t\t");
            out.write("用户名\t\t\t");
            out.write("性别\t\t\t");
            out.write("出生年月\t\t\t");
            out.write("家庭地址\n");
            for(User user:users){
                out.write(user.getUserId()+"\t\t");
                out.write(user.getUserName()+"\t\t");
                out.write(user.getSex()+"\t\t");
                out.write(user.getBirth()+"\t\t");
                out.write(user.getAddress()+"\n");
            }
            System.out.println("用户名单生成！");
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (out != null) {
                try {
                    out.close();
                    out=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
